/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tces.view;
//
//import com.cashmate.model.*;
//import com.cashmate.controller.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tces.controller.UserDAO;
import com.tces.model.User;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;

public class AuthenticationAction extends ActionSupport {
//

    private static final Logger log = Logger.getLogger(AuthenticationAction.class);
    private String userIDNumber, userPassword, userNewPassword, userConfirmPassword;

    @Override
    public String execute() {
        try {
            clearActionErrors();
            User authenticatedUser;
            log.info("ID Number : " + userIDNumber + " Password : " + userIDNumber.trim().length());
            if (!Pattern.matches("\\d{2}-\\d{6}", userIDNumber)) {
                addActionError("Invalid ID Number format.");
                log.error("Invalid ID Number format.");
            }
            if (userPassword.trim().length() <= 7 || userPassword.trim().length() > 32) {
                addActionError("Invalid password length. Password must be 8 - 32 characters.");
                log.error("Invalid password length. Password must be 8 - 32 characters.");
            }
            if (hasActionErrors()) {
                return INPUT;
            }
            clearActionErrors();
            authenticatedUser = UserDAO.authenticate(userIDNumber, userPassword);
            if (authenticatedUser == null) {
                addActionError("ID Number and/or Password is incorrect. Please try again!");
                return INPUT;
            }
            //        HttpSession session = ServletActionContext.getRequest().getSession(true);
            Map session = ActionContext.getContext().getSession();
            session.put("user", authenticatedUser);

            log.info(authenticatedUser.getUserFirstName() + " " + authenticatedUser.getUserLastName() + " of " + authenticatedUser.getCollegeCode() + " is now online");
            ActionContext.getContext().setSession(session);

        } catch (Exception ex) {
            log.error(ex);
        }
        return SUCCESS;
    }

    public String logout() {
        try {
            Map session = ActionContext.getContext().getSession();
            session.remove("user");

        } catch (Exception ex) {
            log.error(ex);
        }
        return SUCCESS;
    }

    public String saveLoginCredential() {
        try {
            User user;
            clearActionErrors();
            user = (User) ActionContext.getContext().getSession().get("user");
            if (!user.getUserIDNumber().equals(userIDNumber)) {
                addActionError("Changing password of other user is not allowed. This incident has been reported.");
                log.error("Changing password of other user is not allowed. This incident has been reported.");
            }
            log.info("ID Number : " + userIDNumber + " Password : " + userIDNumber.trim().length());
            if (!Pattern.matches("\\d{2}-\\d{6}", userIDNumber)) {
                addActionError("Invalid ID Number format.");
                log.debug("Invalid ID Number format.");
            }
            if (userPassword.trim().length() <= 7 || userPassword.trim().length() > 32) {
                addActionError("Invalid password length. Password must be 8 - 32 characters.");
                log.debug("Invalid password length. Password must be 8 - 32 characters.");
            }
            if (userNewPassword.trim().length() <= 7 || userNewPassword.trim().length() > 32) {
                addActionError("Invalid new password length. Password must be 8 - 32 characters.");
                log.debug("Invalid password length. Password must be 8 - 32 characters.");
            }
            if (!userNewPassword.equals(userConfirmPassword)) {
                log.debug("Password doesn't match.");
                addActionError("Password doesn't match.");
            }
            if (hasActionErrors()) {
                return INPUT;
            }
            clearActionErrors();
            log.info("ID Number : " + userIDNumber + " Password : " + userPassword);
            user=null;
            log.info("ID Number : " + userIDNumber + " Password : " + userPassword);
            user = UserDAO.authenticate(userIDNumber, userPassword);
            if (user == null) {
                addActionError("ID Number and/or Password is incorrect. Please try again!");
                return INPUT;
            }
            if (UserDAO.savePassword(userIDNumber, userNewPassword) > 0) {
                addActionMessage("Successfully saved password. Please logout then login again to apply the changes");
            } else {
                addActionError("Unable to save new password. Please check the logfile for more info.");
                return INPUT;
            }

        } catch (Exception ex) {
            log.error(ex);
        }
        return SUCCESS;
    }

    public String getUserConfirmPassword() {
        return userConfirmPassword;
    }

    public void setUserConfirmPassword(String userConfirmPassword) {
        this.userConfirmPassword = userConfirmPassword;
    }

    public String getUserIDNumber() {
        return userIDNumber;
    }

    public void setUserIDNumber(String userIDNumber) {
        this.userIDNumber = userIDNumber;
    }

    public String getUserNewPassword() {
        return userNewPassword;
    }

    public void setUserNewPassword(String userNewPassword) {
        this.userNewPassword = userNewPassword;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

}
